import {makeAutoObservable, reaction} from "mobx";
import dashboardPageStore from "../store/DashboardPageStore";

let authorizationStorageKey = "Authorization";

export class AuthorizationStore {

    authorization: string | null = null


    constructor() {
        makeAutoObservable(this)
        this.authorization = localStorage.getItem(authorizationStorageKey) ?? null
        reaction(() => this.authorization, (arg) => {
            if (arg) {
                localStorage.setItem(authorizationStorageKey, arg)
            } else {
                localStorage.removeItem(authorizationStorageKey)
            }
        })
    }


    setAuthorization(authorization: string | null) {
        this.authorization = authorization
    }

    async logOut() {
        this.authorization = null;
        dashboardPageStore.clear()
    }

    get hasLogIn() {
        return !!this.authorization
    }

}

export default new AuthorizationStore()